package com.seeyon.v3x.rota.controller;

import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.v3x.rota.domain.TResourceDuty;
import com.seeyon.v3x.rota.manager.RotaManager;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class RotaController extends BaseController
{
  private RotaManager rotaManager;

  public RotaManager getRotaManager()
  {
    return this.rotaManager;
  }

  public void setRotaManager(RotaManager rotaManager) {
    this.rotaManager = rotaManager;
  }

  public String getTasks(HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    System.out.println("---------进入成功！----------");
    request.setCharacterEncoding("utf-8");

    String month = request.getParameter("month");
    System.out.println(month + "-");
    List<TResourceDuty> list = this.rotaManager.findByLikeProperty("dutyDate", month + "-");
    JSONArray array = new JSONArray();
    for (TResourceDuty trd : list) {
      JSONObject obj = new JSONObject();
      obj.put("id", trd.getId());
      obj.put("task", trd.getName());
      obj.put("builddate", trd.getDutyDate());
      obj.put("taskType", trd.getType());
      array.add(obj);
    }
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    try
    {
      System.out.println("------json------ " + array.toString());
      response.getWriter().write(array.toString());
    }
    catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }

    return null;
  }

  public String addTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("---------进入增加！----------");
    request.setCharacterEncoding("utf-8");
    String taskInfo = request.getParameter("taskInfo");
    String buildDate = request.getParameter("buildDate");
    String taskType = request.getParameter("taskType");

    TResourceDuty trd = new TResourceDuty();
    trd.setDutyDate(buildDate);
    trd.setType(taskType);
    trd.setName(taskInfo);

    int tempId = this.rotaManager.addTask(trd);
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    try {
      response.getWriter().write("[{\"taskId\":\"" + tempId + "\"}]");
    }
    catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    return null;
  }

  public void updateTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("---------进入更新！----------");
    String taskId = request.getParameter("taskId");
    String taskInfo = request.getParameter("taskInfo");
    String taskType = request.getParameter("taskType");

    TResourceDuty trd = new TResourceDuty();
    trd.setId(new Integer(taskId));
    trd.setType(taskType);
    trd.setName(taskInfo);
    this.rotaManager.updateTask(trd);
  }

  public void deleteTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("---------进入删除！----------");
    String taskId = request.getParameter("taskId");
    TResourceDuty trd = this.rotaManager.findTaskById(Integer.valueOf(taskId));
    this.rotaManager.deleteTask(trd);
  }
}